package com.deliveryservicesystem.app.validation;

import com.deliveryservicesystem.model.Driver;
import com.jgoodies.binding.value.AbstractValueModel;
import com.jgoodies.validation.ValidationResult;
import com.jgoodies.validation.Validator;
import com.toedter.calendar.JDateChooser;
import java.util.Date;

/**
 * File Name: DeliveryValidator.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.presentation.desktop.addpanel
 * Author: Lance
 * Date: Jun 24, 2012
 * Editor:
 */
public class DeliveryValidator extends AbstractValueModel implements Validator<ValidationResult>
{

    private static final long serialVersionUID = 1L;
    private JDateChooser dateChooser;
    private boolean isDelivered;
    private Driver selectedDriver;

    public DeliveryValidator(JDateChooser dateChooser, boolean isDelivered, Object selectedDriver)
    {
        this.dateChooser = dateChooser;
        this.isDelivered = isDelivered;
        this.selectedDriver = (Driver) selectedDriver;
    }

    public JDateChooser getDateChooser()
    {
        return dateChooser;
    }

    public void setDateChooser(JDateChooser dateChooser)
    {
        this.dateChooser = dateChooser;
    }

    public boolean isIsDelivered()
    {
        return isDelivered;
    }

    public void setIsDelivered(boolean isDelivered)
    {
        this.isDelivered = isDelivered;
    }

    public Driver getSelectedDriver()
    {
        return selectedDriver;
    }

    public void setSelectedDriver(Driver selectedDriver)
    {
        this.selectedDriver = selectedDriver;
    }

    @Override
    public Object getValue()
    {
        return this;
    }

    @Override
    public void setValue(Object newValue)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ValidationResult validate(ValidationResult validationTarget)
    {
        if (isDelivered && dateChooser.getDate().after(new Date()))
        {
            validationTarget.addError("Please check date");
            validationTarget.addError("Item delivered date set in future");
        }
        if (!isDelivered && dateChooser.getDate().before(new Date()))
        {
            validationTarget.addError("Please check date");
            validationTarget.addError("Item can not be delivered in the past");
        }
        if (selectedDriver == null)
        {
            validationTarget.addError("Please select a driver number");
        }
        return validationTarget;
    }
}
